﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.fjyc.Integration.HR.Interface;
using NRemedy;
using System.Configuration;

namespace ITSMClient
{
    public class ITSMClient : IEmployeeOperation
    {
        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string ARUid = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string ARPwd = ConfigurationManager.AppSettings["ARPwd"];


        public DateTime GetStartDate()
        {
            return SyncDateHelper.GetStartDate("ITSMSycEmployee");
        }

        public object PushEndDate(DateTimePeroid dt)
        {
            return SyncDateHelper.PushEndDate("ITSMSycEmployee", dt);
        }

        public void PushEmployee(Employee employee)
        {
            //skip null uid
            if (string.IsNullOrEmpty(employee.O_UID))
                return;
            ARProxy<CTMPeople> ctmPeopleARProxy = (ARProxy<CTMPeople>)Server;
            string querystr = string.Format("\'4\'=\"{0}\"", employee.O_UID);
            var exitPeople = ctmPeopleARProxy.GetEntryByQuery(querystr);
            if (exitPeople.Count > 0)
            {
                return;
                //var source = exitPeople[0];
                //if (!string.Equals(employee.COMPANY, source.Company)) source.Company = employee.COMPANY;
                //if (!string.Equals(employee.DEPT, source.Department)) source.Department = employee.DEPT;
                //if (!string.Equals(employee.CNAME[0].ToString(), source.FirstName)) source.FirstName = employee.CNAME[0].ToString();
                //if (!string.Equals(employee.CNAME, source.FullName)) source.FullName = employee.CNAME;
                //if (!string.Equals(employee.CNAME.Substring(1), source.LastName)) source.LastName = employee.CNAME.Substring(1);
                //if (!string.Equals(employee.EMAIL, source.InternetEmail)) source.InternetEmail = employee.EMAIL;
                //if (!string.Equals(employee.POSITIONNAME, source.JobTitle)) source.JobTitle = employee.POSITIONNAME;
                //ctmPeopleARProxy.SetEntry(source);
            }
            else
            {
                CTMPeople people = new CTMPeople
                {
                    Assigned_To = employee.O_UID,
                    //电话需确定
                    Company = employee.COMPANY,
                    //Site值需确定
                    //Site = "",需要AR存在这个Site才能添加
                    //组织没有填写部门
                    //Organization = employee.DEPT,  需要AR存在这个组织才能添加
                    Department = employee.DEPT,
                    FirstName = employee.CNAME.Substring(1),
                    LastName = employee.CNAME.Substring(0, 1),
                    FullName = employee.CNAME,
                    InternetEmail = employee.EMAIL,
                    JobTitle = employee.POSITIONNAME,
                    Short_Description = ".",
                    Submitter = "Demo",
                    PhoneNumberBusiness = "###",
                    ClientType = CTMPeople.ClientType_Enum.office,
                    z1DPassword = "User@123",
                    //only 1 means enabled
                    Status = employee.STATUSCODE == "1" ? CTMPeople.Status_Enum.Enabled : CTMPeople.Status_Enum.Delete
                };
                ctmPeopleARProxy.CreateEntry(people);
            }
        }

        private ARProxy<CTMPeople> _server;
        private ARLoginContext context;

        public object Server
        {
            get
            {
                if (_server == null)
                {
                    context = new ARLoginContext(ARServer, ARUid, ARPwd);
                    _server = new ARProxy<CTMPeople>(context);
                }
                return _server;
            }
        }

        public void Dispose()
        {
            if (context != null) context.Dispose();
        }
    }
}